package com.allwees.bs.c.module.ops.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allwees.bs.c.module.app.dto.AdsVO;
import com.allwees.bs.c.module.ops.dao.AdvertisementDao;
import com.allwees.bs.c.module.ops.service.AdvertisementService;
import com.allwees.bs.core.model.constant.CacheName;
import com.allwees.bs.core.model.ops.AdsModel;
import com.allwees.core.common.constant.EStatus;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/10/19 10:47
 */
@Component
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementDao, AdsModel> implements AdvertisementService {

    @Cacheable(cacheNames = CacheName.OPS_ADS,key = "#model.position.code + '-' + #model.platform + (#model.channelUuid != null ? '-' +  #model.channelUuid : '')",sync = true)
    @Override
    public List<AdsVO> list(AdsModel model) {
        LambdaQueryChainWrapper<AdsModel> lambdaQuery = this.lambdaQuery();
        lambdaQuery.eq(AdsModel::getStatus, EStatus.NORMAL);
        lambdaQuery.eq(AdsModel::getPosition, model.getPosition());
        lambdaQuery.eq(model.getChannelUuid() != null, AdsModel::getChannelUuid, model.getChannelUuid());
        lambdaQuery.eq(AdsModel::getPlatform,model.getPlatform());
        lambdaQuery.orderByDesc(AdsModel::getSort);
        return lambdaQuery.list().stream().map(AdsVO::new).collect(Collectors.toList());
    }
}
